//
//  XGL_FansTableViewCell.swift
//  XGamesLive
//
//  Created by ios on 2024/5/24.
//

import UIKit

class XGL_FansTableViewCell: UITableViewCell {
    var userHeaderView: UIImageView!
    var userNameText: UILabel!
    var unfollowItem: UIButton!
    
    var itemData: FollowAndFansuserData = FollowAndFansuserData(){
        didSet{
            self.userHeaderView.loadImageWithUrl(itemData.header)
            self.userNameText.text = itemData.nickname
            if self.itemData.following == 0{
                self.unfollowItem.isSelected = true
                self.unfollowItem.backgroundColor = UIColor.hexColor("#80FFE6")
            }else{
                self.unfollowItem.isSelected = false
                self.unfollowItem.backgroundColor = UIColor.hexColor("#001310")
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        self.selectionStyle = .none
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension XGL_FansTableViewCell{
    private func createXglView(){
        self.userHeaderView = UIImageView().then({ make in
            make.layerCornerRadius = 25.0
            make.clipsToBounds = true
        })
        self.userNameText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
        })
        self.unfollowItem = UIButton().then({ make in
            make.setTitle("Unfollow", for: .normal)
            make.setTitle("+Follow", for: .selected)
            make.setTitleColor(UIColor.hexColor("#80FFE6"), for: .normal)
            make.setTitleColor(UIColor.hexColor("#001310"), for: .selected)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.backgroundColor = UIColor.hexColor("#001310")
            make.layerCornerRadius = 8.0
        })
        let itemContentView = UIView().then { make in
            make.backgroundColor = UIColor.hexColor("#001310").withAlphaComponent(0.5)
            make.layerCornerRadius = 12.0
        }
        itemContentView.addSubview(self.userHeaderView)
        itemContentView.addSubview(self.userNameText)
        itemContentView.addSubview(self.unfollowItem)
        self.contentView.addSubview(itemContentView)
        
        userHeaderView.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.width.height.equalTo(50.0)
            make.centerY.equalToSuperview()
        }
        userNameText.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(userHeaderView.snp.right).offset(15.0)
        }
        unfollowItem.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-15.0)
            make.width.equalTo(conversionWithScrennWidth(value: 95.0))
            make.height.equalTo(36.0)
        }
        itemContentView.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.centerX.equalToSuperview()
            make.top.equalTo(8.0)
            make.centerY.equalToSuperview()
        }
    }
}
